import SwiftUI

//struct ImageView: Identifiable,Hashable {
//    var id = UUID()
//    var image: Image
//}

struct postBlog: View {
    @State private var image: UIImage?
    @State private var showImagePicker: Bool = false
    @State private var message = ""
    @State private var postimages: [Image] = []
    
    
    @State var policy = OssPolicy(OSSAccessKeyId: "", policy: "", Signature: "", dir: "", host: "", expire: "")

   
    
    
    func handlePostImage(_ image: UIImage?) {
            self.image = image
           print("图片选择完毕")
        //   print(image)
        let filename=getFileName()
        
        
        camera_provider.request(.getOSSPolicy)
        { result in
            switch result {
            case let .success(response):
                do {
                
                    let responseData = response.data
                    print(response)
                // 将JSON对象解码为自定义的数据模型
                //方法：根据返回的数据类型先在Model写对应的struct
                    let decodedObject = try JSONDecoder().decode(OssPolicy.self, from: responseData)
                    policy=decodedObject
                    let image = image!
                    uploadImage(image: image,
                                policy: policy.policy, accessKeyId: policy.OSSAccessKeyId, signature: policy.Signature, dir: policy.dir, host: policy.host, expire: policy.expire){
                        camera_provider.request(.getOSSUrl(filename: filename))
                        { result in
                            switch result {
                            case let .success(response):
                                do {
                                    let decodedUrl = try JSONDecoder().decode(OssUrl.self, from: response.data)
                                    print(decodedUrl)
                                    let url=decodedUrl.url
                                    //将这个url储存并展示 要把https替换成http
                                   
//                                    postimages.append( ImageView(image: showImage(urlString: url)))
                                    
                                }catch let error{
                                    print(error)
                                }
                            case let .failure(error):
                                print(error)
                            }
                            
                        }
                    }
                    
                    
                } catch let error {
                    print(error)
                }
            case let .failure(error):
                print(error)
            }
           
        }
       
       
           showImagePicker=false
        }
    
    var body: some View {
        ScrollView {
            ZStack(alignment: .topLeading) {
                VStack {
                    ZStack(alignment: .bottomTrailing) {
                        // 多行文本框
                        TextEditor(text: $message)
                            .font(.body)
                            .lineSpacing(1)
                            .autocapitalization(.words)
                            .disableAutocorrection(true)
                            .padding()
                            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: UIScreen.main.bounds.height * 0.4)
                            .overlay(
                                HStack() {
//                                    Button(action: {
//                                        // 在这里添加您需要执行的操作，例如拍照或选择照片等
//                                    }, label: {
//                                        Image(systemName: "camera")
//                                            .foregroundColor(.white)
//                                            .padding()
//                                            .background(Color(red: 40/255, green: 212/255, blue: 211/255))
//                                            .clipShape(Rectangle())
//                                    })
//                                    .padding(.leading)
                                    Button(action: {
                                        // 将在此执行操作，例如打开相机或选择图片等
                                        self.showImagePicker = true
                                    }) {
                                        Image(systemName: "camera")
                                            .foregroundColor(.white)
                                            .padding()
                                            .background(Color(red: 40/255, green: 212/255, blue: 211/255))
                                            .clipShape(Rectangle())
                                    }
                                    .padding(.leading)
                                    .sheet(isPresented: $showImagePicker) {
                                        ImagePicker(sourceType: .photoLibrary,completionHandler: self.handlePostImage)
                                    }
//                                    Image("catHome")
//                                        .resizable()
//                                        .frame(width: 70.0, height: 70.0)
                                    //如何遍历展示出Image 很无语
                                    
//                                    ForEach(postimages, id: \.self) { imageView in
//                                        imageView.image.resizable()
//                                            .frame(width: 70.0, height: 70.0)
//                                    }
//                                    Image("p1")
//                                        .resizable()
//                                        .frame(width: 70.0, height: 70.0)
//                                    Image("p1")
//                                        .resizable()
//                                        .frame(width: 70.0, height: 70.0)
                                    
                                }
                                    .frame(maxWidth: .infinity, alignment: .leading)
                                    .padding(.top, UIScreen.main.bounds.height * 0.29)
                                    
                            )
                        
                        // 改变时
                        .onChange(of: message) { _ in
                            _ = message.split { $0 == " " || $0.isNewline }
                        }
                        
                        //边框
                        .overlay(
                            RoundedRectangle(cornerRadius: 8)
                                .stroke(Color(red: 40/255, green: 212/255, blue: 211/255), lineWidth: 2)
                        )
                        
                        // 注释文字
                        if message.isEmpty {
                            Text("请输入内容")
                                .foregroundColor(Color(UIColor.placeholderText))
                                .padding(.bottom, UIScreen.main.bounds.height * 0.35)
                                .padding(.trailing, UIScreen.main.bounds.width * 0.65)
                        }
                        
                    }
                    .padding(.horizontal)
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
                VStack {
                    HStack {
                        Rectangle()
                            .fill(Color.white)
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .stroke(Color.gray)
                            )
                            .overlay(
                                VStack {
                                    HStack {
                                        Text("12")
                                            .font(.largeTitle)
                                            .bold()
                                            .foregroundColor(.black)
                                        Text("Days")
                                            .bold()
                                            .foregroundColor(.gray)
                                    }
                                    Text("您已坚持打卡")
                                        .bold()
                                        .foregroundColor(.black)
                                }
                            )

                            .frame(width: UIScreen.main.bounds.width * 0.45, height: 177.0)
                        VStack {
                            Rectangle()
                                .fill(Color.white)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 8)
                                        .stroke(Color.gray)
                                )
                                .overlay(
                                    VStack {
                                        HStack {
                                            Text("您的计划")
                                                .font(.body)
                                                .bold()
                                                .foregroundColor(Color(red: 40/255, green: 212/255, blue: 211/255))
                                                .frame(maxWidth: 110, alignment: .leading)

                                        }
                                        Text("31天焕肤计划")
                                            .bold()
                                            .foregroundColor(.black)
                                    }
                                )
                                .frame(width: UIScreen.main.bounds.width * 0.45, height: 85.0)
                            Rectangle()
                                .fill(Color.white)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 8)
                                        .stroke(Color.gray)
                                )
                                .overlay(
                                    VStack {
                                        HStack {
                                            Text("您的产品")
                                                .font(.body)
                                                .bold()
                                                .foregroundColor(Color(red: 40/255, green: 212/255, blue: 211/255))
                                                .frame(maxWidth: 110, alignment: .leading)

                                        }
                                        Text("点击选取分享")
                                            .bold()
                                            .foregroundColor(.black)
                                    }
                                )
                                .frame(width: UIScreen.main.bounds.width * 0.45, height: 85.0)
                        }
                    }
                    
                }
                .padding(.top, UIScreen.main.bounds.width * 0.90)
                .padding(.horizontal)
                }
                    VStack(spacing: 30) {
                        Button(action: {}) {
                            HStack(spacing: 10) {   // 创建一个水平堆栈
                                Image(systemName: "message.fill")   // 添加聊天图标
                                Text("参与话题")
                                    .foregroundColor(.primary)
                                    .font(.headline)
                                    .frame(maxWidth: .infinity, alignment: .leading)   // 文字居左显示
                            }
                            .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))   // 添加内边距
                            .background(Color(.systemGray6))   // 设置背景色为灰白色
                            .frame(maxWidth: .infinity)   // 设置按钮长度等于屏幕宽度
                        }
                        
                        Button(action: {}) {
                            HStack(spacing: 10) {   // 创建一个水平堆栈
                                Image(systemName: "heart.fill")   // 添加聊天图标
                                Text("添加心情/状态")
                                    .foregroundColor(.primary)
                                    .font(.headline)
                                    .frame(maxWidth: .infinity, alignment: .leading)   // 文字居左显示
                            }
                            .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))   // 添加内边距
                            .background(Color(.systemGray6))   // 设置背景色为灰白色
                            .frame(maxWidth: .infinity)   // 设置按钮长度等于屏幕宽度
                        }
                        
                        Button(action: {}) {
                            HStack(spacing: 10) {   // 创建一个水平堆栈
                                Image(systemName: "lock.fill")   // 添加聊天图标
                                Text("对谁可见")
                                    .foregroundColor(.primary)
                                    .font(.headline)
                                    .frame(maxWidth: .infinity, alignment: .leading)   // 文字居左显示
                            }
                            .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))   // 添加内边距
                            .background(Color(.systemGray6))   // 设置背景色为灰白色
                            .frame(maxWidth: .infinity)   // 设置按钮长度等于屏幕宽度
                        }
                }
            
                Button(action: {
                    // 发送操作
                }, label: {
                HStack(content: {
                    Text("发送")
                        .font(.title2)
                        .padding(.horizontal, 20.0)
                })
                .padding()
                .foregroundColor(.white)
                .background(Color(red: 40/255, green: 212/255, blue: 211/255))
                .cornerRadius(40)
                })
                .frame(width: 150.0, height: 80.0)
            }
        }
        
        
    
    struct postBlog_Previews: PreviewProvider {
        static var previews: some View {
            postBlog()
        }
    }
}
